/**
 * @file
 * @auth jinguangguo
 * @date 2017/01/19
 */

import tpl from './tpl.html';
import Modal from '../../../../../grsc-base/component/Modal.vue';
import Validator from '../../../../../grsc-base/component/Validator/main';
import service from '../../basic/service';
import VerifyCode from '../../../../component/VerifyCode.vue';

export default Vue.extend({
    name: "closeGoogleDialog",
    template: tpl,
    components: {
        Modal,
        Validator,
        VerifyCode
    },
    data() {
        return {
            form: {
                identifyingCode: '',
                googleCode:''

            },
            rules: {
                identifyingCode: {
                    required: true,
                    requiredTip: this.$t('common.verifyCodeRequireTip'),
                    pattern: '\\d{6}',
                    patternTip: window.CODE.IDENTIFY[this.$i18n.locale]
                },
                googleCode:{
                    required: true,
                    requiredTip: this.$t('common.googleCodeRequireTip'),
                    pattern: '\\d{6}',
                    patternTip: window.CODE.GOOGLE_CODE[this.$i18n.locale]
                }
            },
            isRequesting: false
        };
    },
    props: {
        onSuccess: {
            type: Function,
            default() {

            }
        }
    },
    created(){

    },
    methods: {
        onSendSuccess() {
            this.rules.identifyingCode.$setRight(this.$t('common.codeSentTip'));
        },
        onSendFail(rep) {
            this.rules.identifyingCode.$setError(rep.msg);
        },
        submit(){
            if (this.rules.$checkAll() === false) {
                return;
            }
            this.isRequesting = true;

            let header = {
                'service-code': this.form.identifyingCode,
                'google-code':this.form.googleCode
            };
            service.googleCodeOff({},header).done((rep) => {
                if (rep.code === window.CODE.SUCCESS) {
                    window.success(this.$t('common.success'));
                    this.hide();
                }else if(rep.code === window.CODE.GOOGLE_CODE.CODE){
                    this.rules.googleCode.$setError(window.CODE.GOOGLE_CODE[this.$i18n.locale]);
                }else if(rep.code === window.CODE.IDENTIFY.CODE){
                    this.rules.identifyingCode.$setError(window.CODE.IDENTIFY[this.$i18n.locale]);
                }else {
                    window.error(rep.msg);
                }
            }).always(() => {
                this.isRequesting = false;
            });
        },

        hide() {
            this.$emit('hide');
        }
    }


});